// 连接数据库模块
const mysql = require('mysql')

const db = mysql.createPool({
	host: '127.0.0.1',
	user: 'root',
	password: '123456',
	database: 'final_design'
})

let query = function (sql, values) {
	return new Promise((resolve, reject) => {
		db.query(sql, values, (err, rows) => {
			if (err) {
				reject(err)
			} else {
				resolve(rows)
			}
		})
	})
}

// 测试 mysql模块是否正常工作
db.query('select 1', (err, results) => {
	if (err) return console.log('[Database connection error]: ' + err.message)
	console.log('Database connection succeeded')
})

module.exports = {
	db,
	query
}
